#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n)
    {
        ListNode* PHead = new ListNode(-1);
        PHead->next = head;
        ListNode* prev, * last, * fast;
        prev = PHead;
        last = fast = PHead;
        while (n--)
        {
            if (fast == nullptr)
                break;
            fast = fast->next;
        }
        while (fast != nullptr)
        {
            prev = last;
            last = last->next;
            fast = fast->next;
        }
        prev->next = last->next;
        delete last;
        ListNode* NewHead = PHead->next;
        delete PHead;
        return NewHead;
    }
};

